import base64
import sys
import json
import os
from PyQt5.QtWidgets import QApplication, QMainWindow, QTabWidget, QWidget, QVBoxLayout, QLabel, QProxyStyle, QStyle, \
    QTabBar
from PyQt5.QtGui import QPainter, QPixmap, QIcon
from PyQt5.QtCore import Qt, QRect

from LanguageTool import loadLangDict, getCNfromDict, loadCurLang, getCurLang
from LisenceTool import loadLisenceInfo
from TabBar01 import TabBar01
from TabBar02 import TabBar02
from TabBar03 import TabBar03
from TabBar04 import TabBar04
from TabBar05 import TabBar05
from fileOpTool import check_filesys
from mylog import loggingInit, addLog
from staticPhoto import base64_log_image


def base64_to_icon(base64_data):
    # 解码 Base64 数据
    image_data = base64.b64decode(base64_data)
    # 将二进制数据加载为 QPixmap
    pixmap = QPixmap()
    pixmap.loadFromData(image_data)
    # 将 QPixmap 转换为 QIcon
    return QIcon(pixmap)


class HorizontalTabBarStyle(QProxyStyle):
    def __init__(self, width, height):
        super().__init__()
        self.tab_width = width
        self.tab_height = height

    def drawControl(self, element, option, painter, widget=None):
        if element == QStyle.CE_TabBarTabLabel:
            option_rect = option.rect
            painter.save()

            # Center text horizontally
            painter.translate(option_rect.x(), option_rect.y() + (option_rect.height() / 2))
            painter.rotate(0)  # Keep horizontal

            tab_rect = QRect(0, int(-option_rect.height() / 2), option_rect.width(), option_rect.height())
            text = option.text

            # Use QProxyStyle to draw the tab
            self.drawItemText(painter, tab_rect, Qt.AlignCenter, option.palette, True, text)

            painter.restore()
        else:
            super().drawControl(element, option, painter, widget)

    def sizeFromContents(self, contents_type, option, size, widget=None):
        if contents_type == QStyle.CT_TabBarTab:
            # Set custom width and height
            size.setWidth(self.tab_width)
            size.setHeight(self.tab_height)
        return super().sizeFromContents(contents_type, option, size, widget)


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        # 设置窗口图标
        icon = base64_to_icon(base64_log_image)
        self.setWindowIcon(icon)

        if getCurLang() == 1:
            self.setWindowTitle("UNI-Z Voice Modification Tool V3.0.2【FeatherStar Zone】")
        else:
            self.setWindowTitle("UNI-Z音效修改工具 V3.0.2【星羽空间工作室】")
        # self.setFixedSize(1020, 700)
        self.setMinimumSize(1020, 700)
        self.resize(1020, 700)
        # 创建一个 QTabWidget
        tab_widget = QTabWidget()
        tab_widget.setTabPosition(QTabWidget.West)  # 将选项卡放在左侧

        # 创建自定义的 QTabBar
        tab_bar = QTabBar()
        tab_bar.setShape(QTabBar.RoundedWest)  # 设置形状为左侧圆角
        tab_bar.setStyle(HorizontalTabBarStyle(width=180, height=60))

        # 将自定义的 QTabBar 设置到 QTabWidget 中
        tab_widget.setTabBar(tab_bar)

        # 创建第一个标签页
        tab1 = TabBar01()

        # 创建第二个标签页
        tab2 = TabBar02()

        # 创建第三个标签页
        tab3 = TabBar03()

        # 创建第四个标签页
        tab4 = TabBar04()

        # 创建第5个标签页
        tab5 = TabBar05()

        # 将标签页添加到 QTabWidget 中
        tab_widget.addTab(tab1, getCNfromDict('TabBar01'))
        tab_widget.addTab(tab5, getCNfromDict('TabBar05'))
        tab_widget.addTab(tab2, getCNfromDict('TabBar02'))
        tab_widget.addTab(tab3, getCNfromDict('TabBar03'))
        tab_widget.addTab(tab4, getCNfromDict('TabBar04'))

        # 设置主窗口的中心小部件为 tab_widget
        self.setCentralWidget(tab_widget)

    def closeEvent(self, event):
        addLog('app', 'info', 'APP close!')


if __name__ == "__main__":
    check_filesys()
    loggingInit()

    # ffmpeg环境引入
    current_path = os.path.abspath(os.getcwd())
    # current_path.replace('/', '//')
    ffmpeg_path = current_path + "\\" + "ffmpeg-7.1-essentials_build\\bin"
    os.environ["PATH"] += os.pathsep + ffmpeg_path
    # print(os.environ["PATH"])

    if not os.path.exists(ffmpeg_path):
        addLog('app', 'warn', 'ffmpeg not found!')
        exit(0)

    loadCurLang()
    if loadLangDict() != 0:
        exit(0)

    loadLisenceInfo()
    addLog('app', 'info', 'Get Lisence Info!')

    QApplication.setHighDpiScaleFactorRoundingPolicy(Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
    # 适应高DPI设备
    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
    # 解决图片在不同分辨率显示模糊问题
    QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)

    app = QApplication(sys.argv)
    # 可以在此处设置应用程序图标
    icon = base64_to_icon(base64_log_image)
    app.setWindowIcon(icon)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit(app.exec_())
